package com.apanal.qlife.advice.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.advice.dao.AdviceMapper;
import com.apanal.qlife.advice.dao.AdviceOrgAreaRelMapper;
import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.model.AdviceOrgAreaRel;
import com.apanal.qlife.advice.service.SysAnnounceService;
import com.apanal.qlife.common.constants.AdviceConstants;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.Role;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.RoleService;

@Service
public class SysAnnounceServiceImpl implements SysAnnounceService {

	@Autowired
	AdviceMapper mapper;

	@Autowired
	AdviceOrgAreaRelMapper adrmapper;

	@Autowired
	private RoleService roleService;

	@Autowired
	private OrganizationService orgService;

	@Override
	public PageReturn<Advice> findAdviceBycond(Advice advice, Page page) {
		int count = mapper.selectRelationForPageCount(advice, page);
		List<Advice> list = mapper.selectRelationForPage(advice, page);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public String insert(Advice advice, User user) throws ParseException {
		// 总后台管理员
		advice.setAdvId(IdWorkerFactory.nextId());
		advice.setAdvType(AdviceConstants.SYS_ANNOUNCE);
		Long platOrgId = advice.getPlatOrgId();
		advice.setInsertBy(user.getUseId());
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		advice.setStartDate(date);
		advice.setNormal(Constants.NORMAL_TRUE);
		advice.setStatus(Constants.IS_VALID_TRUE);
		advice.setSetType(AdviceConstants.SET_TYPE_ORG);
		if (platOrgId == -1) {
			advice.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_ALL_PLATFORM);
		} else {
			advice.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
		}
		advice.setLastUpdateBy(user.getUseId());
		mapper.insert(advice);

		if (platOrgId != -1) {
			AdviceOrgAreaRel adr = new AdviceOrgAreaRel();
			adr.setAdvId(advice.getAdvId());
			adr.setInsertBy(user.getUseId());
			adr.setRelId(IdWorkerFactory.nextId());
			// 总后台管理员（todo）
			Role role = roleService.findRoleNameByid(user.getUseId());
			if (role != null) {
				adr.setOrgId(advice.getPlatOrgId());
			} else {
				adr.setOrgId(user.getOrgId()); // 平台管理员
			}
			adr.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
			adrmapper.insert(adr);

		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Advice> findAppAnnouceList(Advice advice, Page page) {
		int count = mapper.selectAppForPageCount(advice, page);
		List<Advice> list = mapper.selecAppForPage(advice, page);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public Advice findAppAnnouceDtail(Advice advice) {

		return mapper.selectAppAnnouceDtail(advice);
	}

	@Override
	public String insertAdviceOrgRel(AdviceOrgAreaRel adr) {
		adrmapper.insert(adr);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertAdvice(Advice advice) {
		mapper.insert(advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Advice findDetailById(Long advId) {

		return mapper.selectByPrimaryKey(advId);
	}

	@Override
	public String updateAppAonnce(Advice ac, User user, Advice advice)
			throws ParseException {

		// 更新公告表
		int oldSetTypeDetail = ac.getSetTypeDetail();
		ac.setLastUpdateBy(user.getUseId());
		ac.setAdvDetails(advice.getAdvDetails());
		ac.setAdvTitle(advice.getAdvTitle());
		ac.setSetTypeDetail(advice.getSetTypeDetail());
		ac.setSetType(advice.getSetType());
		ac.setPutInVersion(advice.getPutInVersion());
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		ac.setStartDate(date);
		mapper.updateByPrimaryKey(ac);
		int setTypeDetalis = advice.getSetTypeDetail();
		// 新增中间表
		// 删除之间投放数据
		Advice a = new Advice();
		a.setAdvId(ac.getAdvId());
		a.setSetTypeDetail(oldSetTypeDetail);
		adrmapper.deletCurrentOrel(a);
		int setType = advice.getSetType();
		if (setTypeDetalis != 11 && setType == 1) { // 推送方式非全部机构
			String orgIds = advice.getOrgIds();
			if (orgIds != null && !orgIds.equals("")) {
				String[] ary = orgIds.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long orgid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					if (setTypeDetalis == 15) {
						arog.setComId(orgid);
					} else {
						arog.setOrgId(orgid);
					}
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					arog.setRelId(IdWorkerFactory.nextId());
					adrmapper.insert(arog);
				}
			}
		} else if (setTypeDetalis != 21 && setType == 2) {
			String areids = advice.getOrgIds();
			if (areids != null && !areids.equals("")) {
				String[] ary = areids.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long areid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					if (setTypeDetalis == 22) {
						arog.setProvinceId(areid);

					} else if (setTypeDetalis == 23) {
						arog.setCityId(areid);
					} else if (setTypeDetalis == 24) {
						arog.setAreaId(areid);
					}
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					arog.setRelId(IdWorkerFactory.nextId());
					adrmapper.insert(arog);
				}
			}

		}

		mapper.updateByPrimaryKey(ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateAnnounce(Advice advice, User user, Advice ac)
			throws ParseException {
		int oldSetTypeDetail = ac.getSetTypeDetail();
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		ac.setStartDate(date);
		ac.setLastUpdateBy(user.getUseId());
		ac.setAdvDetails(advice.getAdvDetails());
		ac.setAdvTitle(advice.getAdvTitle());
		Advice a = new Advice();
		if (oldSetTypeDetail == 12) {
			if (advice.getPlatOrgId() == -1) {
				ac.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_ALL_PLATFORM);
				mapper.updateByPrimaryKey(ac);
				a.setAdvId(ac.getAdvId());
				a.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
				adrmapper.deletCurrentOrel(a);
			} else if (advice.getPlatOrgId() != -1) {
				mapper.updateByPrimaryKey(ac);
				adrmapper.deletCurrentOrel(a);
				AdviceOrgAreaRel adr = new AdviceOrgAreaRel();
				adr.setRelId(IdWorkerFactory.nextId());
				adr.setAdvId(advice.getAdvId());
				adr.setInsertBy(user.getUseId());
				adr.setOrgId(advice.getPlatOrgId());
				adr.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
				adrmapper.insert(adr);

			}

		}
		if (oldSetTypeDetail == 16) {

			if (advice.getPlatOrgId() == -1) {
				ac.setSetTypeDetail(16);
				mapper.updateByPrimaryKey(ac);

			} else if (advice.getPlatOrgId() != -1) {
				ac.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
				mapper.updateByPrimaryKey(ac);
				AdviceOrgAreaRel adr = new AdviceOrgAreaRel();
				adr.setAdvId(advice.getAdvId());
				adr.setInsertBy(user.getUseId());
				adr.setRelId(IdWorkerFactory.nextId());
				adr.setOrgId(advice.getPlatOrgId());
				adr.setSetTypeDetail(AdviceConstants.SET_TYPE_DETAIL_PLATFORM);
				adrmapper.insert(adr);

			}
		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Advice> findAdviceBycondNo(Advice advice, Page page) {
		int count = mapper.selectRelationForPageCountNo(advice, page);
		List<Advice> list = mapper.selectRelationForPageNo(advice, page);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public PageReturn<Advice> findAppAnnouceListNo(Advice advice, Page page) {
		int count = mapper.selectAppForPageCountNo(advice, page);
		List<Advice> list = mapper.selecAppForPageNo(advice, page);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public String modifyAnnounce(Advice ac) {
		mapper.updateByPrimaryKey(ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String deleteAnnounce(Advice ac) {
		// mapper.updateByPrimaryKey(ac);
		mapper.deleteByPrimaryKey(ac.getAdvId());
		adrmapper.deleteByPrimaryKey(ac.getAdvId());
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertAppAdvice(Advice advice, User user)
			throws ParseException {
		// 新增公告表
		advice.setAdvType(AdviceConstants.APP_ANNOUNCE);
		advice.setInsertBy(user.getUseId());
		advice.setAdvId(IdWorkerFactory.nextId());
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		advice.setStartDate(date);
		advice.setLastUpdateBy(user.getUseId());
		advice.setNormal(Constants.NORMAL_TRUE);
		advice.setStatus(Constants.IS_VALID_TRUE);
		mapper.insert(advice);
		int setTypeDetalis = advice.getSetTypeDetail();
		// 新增中间表
		int setType = advice.getSetType();
		if (setTypeDetalis != 11 && setType == 1) { // 推送方式非全部机构
			String orgIds = advice.getOrgIds();
			if (orgIds != null && !orgIds.equals("")) {
				String[] ary = orgIds.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long orgid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					if (setTypeDetalis == 15) {
						arog.setComId(orgid);
					} else {
						arog.setOrgId(orgid);
					}
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					arog.setRelId(IdWorkerFactory.nextId());
					adrmapper.insert(arog);
				}
			}
		} else if (setTypeDetalis != 21 && setType == 2) {
			String areids = advice.getOrgIds();
			if (areids != null && !areids.equals("")) {
				String[] ary = areids.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long areid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					if (setTypeDetalis == 22) {
						arog.setProvinceId(areid);

					} else if (setTypeDetalis == 23) {
						arog.setCityId(areid);
					} else if (setTypeDetalis == 24) {
						arog.setAreaId(areid);
					}
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					arog.setRelId(IdWorkerFactory.nextId());
					adrmapper.insert(arog);
				}
			}

		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String modifyAppAonnce(Advice ac) {
		mapper.updateByPrimaryKey(ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String deleteAppAonnce(Advice ac) {
		mapper.deleteByPrimaryKey(ac.getAdvId());
		adrmapper.deleteByadvId(ac.getAdvId());
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Advice> findAonnce(Long orgId, Page page, Advice advice) {
		Organization org = new Organization();
		org.setOrgId(orgId);
		Organization orgz = orgService.findOrganizationById(org);
		String orgPIds = orgz.getOrgPIds() + orgz.getOrgId().toString();
		org.setOrgPIds(orgPIds);
		int count = mapper.selectAnnForPageCount(advice, page, org);
		List<Advice> list = mapper.selectAnnForPage(advice, page, org);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	/**
	 * 首页查询公告详情的接口
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public Advice findSysAnnounceDtail(Long advId) {
		Advice advice = mapper.selectByPrimaryKey(advId);
		return advice;
	}

	@Override
	public PageReturn<Advice> findAdvice(Advice advice, Organization org,
			Page page) {

		int count = mapper.selectMessAonnucePageCount(advice, page, org);
		List<Advice> list = mapper.selectMessAonnucePage(advice, page, org);
		return new PageReturn<Advice>(list, count, page, advice);
	}

}